package com.yiba.vpn.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import com.stripe.Stripe;
import com.stripe.exception.*;
import com.stripe.model.Charge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.wordnik.swagger.annotations.ApiOperation;
import com.yiba.vpn.authorization.manager.TokenManager;
import com.yiba.vpn.config.Constants;
import com.yiba.vpn.domain.User;
import com.yiba.vpn.model.ResultModel;
import com.yiba.vpn.service.InviteService;
import com.yiba.vpn.util.RegexUtil;

/** 
* @author : tang 
* @createtime ：2018年1月29日 上午11:54:20 
* 
*/
@RestController
@RequestMapping("/i")
public class InviteController {
	private Logger logger = LoggerFactory.getLogger(InviteController.class);
	
	@Autowired
	private InviteService inviteService;
	
	@Autowired
    private TokenManager tokenManager;
	
	@RequestMapping(value = "/do",method = RequestMethod.GET)
	@ApiOperation(value = "执行邀请跳转")
	@ResponseBody
	public ModelAndView doinvite(HttpServletRequest request,ModelAndView mv) {
		String sign = request.getParameter("sign");
		logger.info("访问的 host ="+sign);
		mv = new ModelAndView();
		if(sign != null && RegexUtil.isVpnId(sign)) {
			sign = inviteService.doinvite(sign);
		}
		String uuid = UUID.randomUUID().toString().replace("-", "");
		tokenManager.addV(request.getRequestedSessionId()+"", uuid, Constants.REGISTER_EXPIRES_SECONDS, TimeUnit.SECONDS);
		mv.addObject("uuid",uuid);
		mv.addObject("invitationcode", sign);
		mv.setViewName("login");
		return mv;
	}
	
	
	
	@RequestMapping(value = "/show", method = RequestMethod.POST)
	@ApiOperation(value = "生成邀请连接")
	@ResponseBody
	public ResponseEntity<ResultModel> showInviteInfo(@RequestBody User user) {
		return inviteService.showInviteInfo(user);
	}
	
	@RequestMapping(value = "/stripePay", method = RequestMethod.GET)
	@ApiOperation(value = "")
	@ResponseBody
	public void test(HttpServletRequest request) {
		Stripe.apiKey = "sk_test_tj2czy150aMraTyvGpdVgleG";
	// Token is created using Checkout or Elements!
// Get the payment token ID submitted by the form:
		String token = request.getParameter("stripeToken");

// Charge the user's card:
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("amount", 999);
		params.put("currency", "hkd");
		params.put("description", "Example charge");
		params.put("source", token);

		try {
			Charge charge = Charge.create(params);
		} catch (AuthenticationException e) {
			e.printStackTrace();
		} catch (InvalidRequestException e) {
			e.printStackTrace();
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (CardException e) {
			e.printStackTrace();
		} catch (APIException e) {
			e.printStackTrace();
		}
	}

	
	

}
 